package service

import (
	"fmt"
	"singo/model"
	"singo/serializer"
)

// VersionCreateService 管理新增版本的服务
type VersionCreateService struct {
	ID      string `form:"id" json:"id" binding:""`
	Commit  string `form:"commit" json:"commit" binding:""`
	Author  string `form:"author" json:"author" binding:""`
	Email   string `form:"email" json:"email" binding:`
	Date    string `form:"date" json:"date" binding:""`
	Content string `form:"content" json:"content" binding:""`
}

// VersionCreate 新增版本
func (service *VersionCreateService) VersionCreate() *serializer.Response {
	fmt.Print(service)
	version := model.Version{
		UID:     service.ID,
		Commit:  service.Commit,
		Author:  service.Author,
		Email:   service.Email,
		Date:    service.Date,
		Content: service.Content,
	}

	count := 0
	model.DB.Model(&model.Version{}).Where("name = ?", service.ID).Count(&count)
	if count > 0 {
		return &serializer.Response{
			Status: 40001,
			Msg:    "版本已存在",
		}
	}

	// 创建版本
	if err := model.DB.Create(&version).Error; err != nil {
		return &serializer.Response{
			Status: 50001,
			Msg:    "版本保存失败",
			Error:  err.Error(),
		}
	}
	return &serializer.Response{
		Data: serializer.BuildVersion(version),
	}
}
